﻿using QuanLyNhaSach.BusinessLogicLayer;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuanLyQuyDinh_NS
{
    public partial class QuanLyQuyDinh : Form
    {
        public QuanLyQuyDinh()
        {
            InitializeComponent();
        }


        private void DangNhap_Click(object sender, EventArgs e)
        {
            QuanLyUserBLL a = new QuanLyUserBLL();

            DataTable kt = a.DangNhap(txt_name.Text, txt_matkhau.Text);
            bool done = true;
            while (kt.Rows.Count == 0)
            {
                a.DangNhap(txt_name.Text, txt_matkhau.Text);
                MessageBox.Show("Tài khoản hoặc mật khẩu không đúng, Vui lòng nhập lại!");
                txt_name.Clear();
                txt_matkhau.Clear();
                done = false;
                if (!done) return;
            };
            DangNhap.Enabled = false;
            MessageBox.Show("Đăng nhập thành công");
            txt_name.Enabled = false;
            txt_matkhau.Enabled = false;
            txt_luong_nhap_toi_thieu.Enabled = true;
            txt_luong_ton_toi_thieu.Enabled = true;
            txt_no_toi_da.Enabled = true;
            txt_luong_ton_ban.Enabled = true;
            checkBox1.Enabled = true;
            DongY.Enabled = true;
        }

        private void QuanLyQuyDinh_Load(object sender, EventArgs e)
        {
            QuanLyQuyDinhBLL qd = new QuanLyQuyDinhBLL();
            DataTable data = qd.LayQuyDinh();
            string check = data.Rows[0]["QuyDinh4"].ToString();
            txt_luong_nhap_toi_thieu.Text = data.Rows[0]["LuongNhapToiThieu"].ToString();
            txt_luong_ton_toi_thieu.Text = data.Rows[0]["LuongTonNhap"].ToString();
            txt_luong_ton_ban.Text = data.Rows[0]["LuongTonBan"].ToString();
            txt_no_toi_da.Text = data.Rows[0]["NoToiDa"].ToString();
            if (check == "False")
                checkBox1.Checked = false;
            else
                checkBox1.Checked = true;
            //checkBox1.DataBindings.Add("bool", data, "QuyDinh4");
            //checkBox1.Checked = true;
        }

        private void DongY_Click(object sender, EventArgs e)
        {
            QuanLyQuyDinhBLL a = new QuanLyQuyDinhBLL();
            DataTable kt = a.LayQuyDinh();
            bool done = true;
            if (txt_luong_nhap_toi_thieu.Text == "" || txt_luong_ton_ban.Text == "" || txt_luong_ton_toi_thieu.Text == "" || txt_no_toi_da.Text == "")
                done = false;
            if (!done)
            {
                MessageBox.Show("Một hoặc nhiều ô chưa được nhập");
                return;
            }
            else
            {
                a.ThayDoiQuyDinh(int.Parse(txt_no_toi_da.Text), int.Parse(txt_luong_ton_ban.Text), int.Parse(txt_luong_ton_toi_thieu.Text), int.Parse(txt_luong_nhap_toi_thieu.Text), checkBox1.Checked);
                MessageBox.Show("Cập nhật quy định thành công");
            }
        }
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        string old_text = "";
        private void txt_luong_nhap_toi_thieu_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int a = int.Parse(txt_luong_nhap_toi_thieu.Text);
                old_text = txt_luong_nhap_toi_thieu.Text;
            }
            catch (FormatException)
            {
                txt_luong_nhap_toi_thieu.Text = old_text;
            }
        }

        private void txt_luong_ton_toi_thieu_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int a = int.Parse(txt_luong_ton_toi_thieu.Text);
                old_text = txt_luong_ton_toi_thieu.Text;
            }
            catch (FormatException)
            {
                txt_luong_ton_toi_thieu.Text = old_text;
            }
        }

        private void txt_luong_ton_ban_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int a = int.Parse(txt_luong_ton_ban.Text);
                old_text = txt_luong_ton_ban.Text;
            }
            catch (FormatException)
            {
                txt_luong_ton_ban.Text = old_text;
            }
        }

        private void txt_no_toi_da_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int a = int.Parse(txt_no_toi_da.Text);
                old_text = txt_no_toi_da.Text;
            }
            catch (FormatException)
            {
                txt_no_toi_da.Text = old_text;
            }
        }

        private void HuyBo_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        


    }
}
